import java.util.HashSet;
import java.util.Set;

public class Solution859 {
    public boolean buddyStrings(String s, String goal) {
        if (s.length()!=goal.length()){
            return false;
        }
        int count=0;
        char[] chars=new char[100];
        for (int i=0;i<s.length();i++){
            if (s.charAt(i)!=goal.charAt(i)){
                chars[count*2]=s.charAt(i);
                chars[count*2+1]=goal.charAt(i);
                count++;
            }
        }
        if (count!=0&&count!=2){
            return false;
        }
        if (count==0){
            Set<Character> set=new HashSet();
            int flag=0;
            for (int i=0;i<s.length();i++){
                if (set.contains(s.charAt(i))){
                    flag=1;
                }
                set.add(s.charAt(i));
            }
            return flag==0?false:true;
        }
        return chars[0]==chars[3]&&chars[1]==chars[2]?true:false;
    }

    public static void main(String[] args) {
        System.out.println(new Solution859().buddyStrings("ab","ba"));
    }
}
